package com.cgly.medical.controller;

import com.cgly.medical.annotation.WxlRateLimiter;
import com.cgly.medical.domain.Doctor;
import com.cgly.medical.domain.Patient;
import com.cgly.medical.mapper.PatientMapper;
import com.cgly.medical.param.PatientHistoryParam;
import com.cgly.medical.service.DoctorService;
import com.cgly.medical.service.PatientHistoryService;
import com.cgly.medical.service.PatientService;
import com.cgly.medical.util.MailClient;
import com.cgly.medical.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 黄浩然
 */
@RestController
@RequestMapping("/api/patient_history")
public class PatientHistoryController {

    @Autowired
    private PatientHistoryService patientHistoryService;

    @Autowired
    MailClient mailClient;


    @Resource
    PatientMapper patientMapper;

    @Resource
    DoctorService doctorService;


    @PostMapping("/{current}/{limit}")
    public Msg getAllPatientHistory(@PathVariable("current") int pn, @PathVariable("limit") int size,
                                    @RequestBody(required = false) PatientHistoryParam patientHistoryParam) {
        patientHistoryParam.setPn(pn);
        patientHistoryParam.setSize(size);
        Msg msg = patientHistoryService.getAllPatientHistory(patientHistoryParam);
        return msg;
    }


    @PostMapping("/time/{time}")
    public Msg addPatientHistory(@PathVariable String time,@RequestBody PatientHistoryParam patientHistoryParam) {
        Msg msg = patientHistoryService.addPatientHistory(Integer.valueOf(time),patientHistoryParam);
        Patient patientById = patientMapper.getPatientById(patientHistoryParam.getPatientId());
        Doctor doctor = doctorService.selectDoctorByid(patientHistoryParam.getDoctorId().toString());
        mailClient.sendMail(patientById.getEmail(),"创新骨科医院",patientById.getPname()+"您好！" +
                "您已成功预约"+doctor.getName()+"医生，请您到301科室就诊!");
        return msg;
    }

    @GetMapping("/{id}")
    public Msg getPatientHistoryById(@PathVariable("id") Long id) {
        Msg msg= patientHistoryService.getPatientHistory(id);
        return msg;
    }

    @PostMapping("/{id}")
    public Msg updatePatientHistory(@PathVariable("id") Long id,
                                    @RequestBody PatientHistoryParam patientHistoryParam) {
        patientHistoryParam.setId(id);
        if (!StringUtils.hasLength(patientHistoryParam.getMessage())) {
            return Msg.fail().mess("病历内容不能为空");
        }
        Msg msg = patientHistoryService.updatePatientHistory(patientHistoryParam);
        return msg;
    }

    @DeleteMapping("/{id}")
    public Msg deletePatientHistory(@PathVariable("id") Long id) {
        Msg msg = patientHistoryService.deletePatientHistory(id);
        return msg;
    }

    @PostMapping("/byDoctorId/{current}/{limit}")
    public Msg getPatientHistoryByDoctorId(@PathVariable("current") int pn, @PathVariable("limit") int size,
                                           @RequestBody(required = false) PatientHistoryParam patientHistoryParam) {
        patientHistoryParam.setPn(pn);
        patientHistoryParam.setSize(size);
        Msg msg = patientHistoryService.getPatientHistoryByDoctorId(patientHistoryParam);
        return msg;

    }
}
